package org.oyyh.dao;

import cn.hutool.crypto.SecureUtil;
import org.oyyh.pojo.User;
import org.oyyh.util.DbUtilHikari;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户操作类
 *
 * @author 欧阳远辉
 * @version 1.0
 */
public class UserDao {
    /**
     * 用户注册
     *
     * @param username 用户姓名
     * @param userPwd  用户密码
     * @return 注册成功返回1，失败返回0
     */
    public int insertUser(String username, String userPwd) {
        int result = 0;
        // 数据库连接对象
        Connection conn;
        // 数据库预编译对象
        PreparedStatement pst = null;

        // 获取数据库连接
        conn = DbUtilHikari.getConnection();
        String sql = "INSERT INTO users(username, userpwd) VALUES (?, ?)";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setString(2, SecureUtil.md5(userPwd));
            result = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(pst, conn);
        }

        return result;
    }

    /**
     * 用户登录功能
     *
     * @param username 用户名
     * @param userPwd  用户密码
     * @return 登录成功返回true，失败返回false
     */
    public boolean login(String username, String userPwd) {
        boolean result = false;
        // 数据库连接对象
        Connection conn;
        // 数据库预编译对象
        PreparedStatement pst = null;
        // 结果集对象
        ResultSet rs = null;

        // 获取数据库连接
        conn = DbUtilHikari.getConnection();
        String sql = "SELECT * FROM users WHERE username=? AND userpwd=?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setString(2, SecureUtil.md5(userPwd));
            rs = pst.executeQuery();
            if (rs.next()) {
                result = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(rs, pst, conn);
        }
        return result;
    }

    /**
     * 根据用户编号删除用户
     *
     * @param userid 用户id
     * @return 删除成功返回true，删除失败返回false
     */
    public int delUser(int userid) {
        int result = 0;
        // 数据库连接对象
        Connection conn;
        // 数据库预编译对象
        PreparedStatement pst = null;

        // 获取数据库连接
        conn = DbUtilHikari.getConnection();
        String sql = "DELETE FROM users WHERE userid=?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, userid);
            result = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(pst, conn);
        }
        return result;
    }


    /**
     * 修改用户信息
     *
     * @param userid   用户编号
     * @param username 用户名
     * @param userPwd  用户密码
     * @return 修改成功返回1，失败返回0
     */
    public int modifyUser(int userid, String username, String userPwd) {
        int result = 0;
        // 数据库连接对象
        Connection conn;
        // 数据库预编译对象
        PreparedStatement pst = null;

        // 获取数据库连接
        conn = DbUtilHikari.getConnection();
        String sql = "UPDATE users SET username=?, userpwd=? WHERE userid=?";
        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, username);
            pst.setString(2, SecureUtil.md5(userPwd));
            pst.setInt(3, userid);
            result = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(pst, conn);
        }
        return result;
    }

    /**
     * 查询所有用户信息
     * @return 包含所有用户信息的列表
     */
    public List<User> selectAllUsers() {
        List<User> userList = new ArrayList<User>();
        // 数据库连接对象
        Connection conn;
        // 数据库预编译对象
        PreparedStatement pst = null;
        // 结果集对象
        ResultSet rs = null;

        // 获取数据库连接
        conn = DbUtilHikari.getConnection();
        String sql = "SELECT * FROM users";
        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setUserId(rs.getInt("userid"));
                user.setUsername(rs.getString("username"));
                user.setUserPwd(rs.getString("userpwd"));
                userList.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtilHikari.close(rs, pst, conn);
        }
        return userList;
    }
}
